package com.sqj.proxyPattern.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @ClassName: CglibMeipo
 * @Description: TODO
 * @Author: songqijun
 * @Date: 2019/11/3 11:05 下午
 * @Version: 1.0
 **/
public class CglibMeipo implements MethodInterceptor {
    public Object getInstance(Class<?> clazz) throws Exception{
        Enhancer enhancer = new Enhancer();
        //要把哪个设置为即将生成的新类的父类
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        before();
        Object obj = methodProxy.invokeSuper(o, objects);
        after();
        return obj;
    }

    public void before(){
        System.out.println("我是媒婆：现在已确认你的需求");
        System.out.println("开始物色");
    }

    public void after(){
        System.out.println("如果合适的话，就准备了");
    }
}
